Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) translate-c: handle bitfield #14068

Closed
wants to merge 1 commit into from
Closed

Conversation

iacore
Copy link
Contributor

@iacore iacore commented Dec 25, 2022

Because I don't know Clang, I left 1 TODO in the code

fix #1499

  • custom padding (implementation defined)
  • padding
  • unnamed field

@iacore iacore changed the title translate-c: handle bitfield (WIP) translate-c: handle bitfield Dec 25, 2022
Copy link
Member

@Vexu Vexu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zig's integer backed packed structs are not compatible with C structs and cannot be directly used to implement structs with bitfields.

@iacore
Copy link
Contributor Author

iacore commented Dec 27, 2022

Thanks!

Turns out struct size with bitfield is implementation defined.

The following on GCC yields different values.

#include <stdio.h>

struct S1 {
    unsigned char b1 : 1;
    //unsigned :0; // #1. start a new byte
    unsigned char b2 : 1;
};

struct S2 {
    unsigned int b1 : 1;
    unsigned int b2 : 1;
};

int main() {
    printf("%d\n", sizeof(char)); 
    printf("%d\n", sizeof(int)); 
    printf("%d\n", sizeof(struct S1)); // usually prints 1
    printf("%d\n", sizeof(struct S2)); // usually prints 4
}

@iacore iacore changed the title translate-c: handle bitfield (WIP) translate-c: handle bitfield Dec 27, 2022
@Vexu
Copy link
Member

Vexu commented Dec 27, 2022

Turns out struct size with bitfield is implementation defined.

Yup, this is all correctly implemented by https://github.com/Vexu/arocc and the plan is to make translate-c use it instead of Clang.

@iacore
Copy link
Contributor Author

iacore commented Dec 27, 2022

Page 14: https://www.uclibc.org/docs/psABI-x86_64.pdf says:

  • bit-fields are allocated from right to left
  • bit-fields must be contained in a storage unit appropriate for its declared type
  • bit-fields may share a storage unit with other struct / union members
  • Unnamed bit-fields’ types do not affect the alignment of a structure or union. (used to separate storage unit only)

From experimenting with GCC:

  • char:1 + char:1 = 1 byte (struct S1 above)
  • char:1 + int:1 = 4 bytes
  • int:1 + char:1 = 4 bytes
  • int:1 + int:1 = 4 bytes (struct S2 above)

@iacore iacore closed this Dec 27, 2022
@iacore iacore mentioned this pull request Dec 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

translate-c: support C bitfields
2 participants